ggplot2 basics

Data Visualization

Javier Tamayo-Leiva

22 December 2020

Tidyverse


ggplot2


ggplot2 basics

Index

  • ggplot2 Package
  • Basic Plot
  • Layers
  • Aesthetic Mappings
  • Geom Objects
  • Statistical Transformation
  • Position Adjustments
  • Extra Settings

Tip





The slides are based on practical learning and the philosophy of free knowledge. To do so, all the command lines are functional and thought to be copied and pasted into your R session. The dataset required is already loaded into the ggplot2 package. Throughout each plot’s completeness, you will also gain basic knowledge of R language, ggplot2, and data visualization.



ggplot2-package

You can load ggplot2 with tidyverse or independently into your R session.
For more info, you can type into your R session
help(“ggplot2”)

The Dataset (ggplot2::diamonds)

Prices of over 50,000 round cut diamonds.

## # A tibble: 6 x 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
## 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
## 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
## 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
## 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
## 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
## [1] 53940    10

Basic plot

price : price in US dollars ($326–$18,823)
carat : weight of the diamond (0.2–5.01)

Basic command

  • data : Default dataset to use for plot (diamonds)

  • mapping : Default list of aesthetic mappings (visual properties) to use for plot (x= carat, y= price). If not specified, must be supplied in each layer added to the plot

ggplot2 layers



A layer combines data, aesthetic mapping, geom (geometric object), stat (statistical transformation), and a position adjustment. Rendering a layer, you decide the type of ggplot2 object to create. However, the layers you can apply will depend on your data and geom object you select.

Layers can also combine different datasets into one unique ggplot2 object. This feature allows you to create sophisticated plots.

You can apply the officials’ layers that are already loaded into ggplot2 packages or used ggplot2 extensions, which are into other R packages.

ggplot2 Aesthetic mappings

Required aesthetic

  • X
  • Y (Two variables)

Extra aesthetic

  • Color or Colour
  • Fill
  • Size
  • Shape
  • Linetype
  • Alpha (transparency)
  • Group
  • Label
  • Stroke…












Learn more about these options by typing in your R session.
vignette(“ggplot2-specs”)

Plot Aesthetics

Plot Aesthetics variables

Geom Objects

One variable

  • geom_bar()
  • geom_histogram()
  • geom_density()


Two variables

  • geom_point()
  • geom_line()
  • geom_smooth()


Three variables

  • geom_contour()
  • geom_tile()
  • geom_raster()

Display error

  • geom_crossbar()
  • geom_errorbar()
  • geom_linerange()
  • geom_pointrange()

Plot geoms (Two variables)

Plot geoms (One and Three variables)

Plot joined layers

Statistical Transformation


Stat

  • stat_count()
  • stat_bin_2d()
  • stat_boxplot()
  • stat_contour()
  • stat_contour_filled()
  • stat_density()
  • stat_bin()
  • stat_qq()

Geom

  • geom_bar()
  • geom_bin2d()
  • geom_boxplot()
  • geom_contour()
  • geom_contour_filled()
  • geom_density()
  • geom_histogram()
  • geom_qq()











Statistical Transformation Options

Statistical Transformation

Statistical Transformation

Tip




Consider the differential settings between geoms and stat elements

stat_density(
mapping = NULL,
data = NULL,
geom = “area”,
position = “stack”,
…)

geom_density(
mapping = NULL,
data = NULL,
stat = “density”,
position = “identity”,
…)

Position Adjustments


Position

  • position_dodge()
  • position_dodge2()
  • position_identity()
  • position_jitter()
  • position_jitterdodge()
  • position_stack()
  • position_fill()
  • position_nudge()

Geom

  • “dodge”
  • “dodge2”
  • “identity”
  • “jitter”
  • position_jitterdodge()
  • “stack” (counts)
  • “fill” (proportion)
  • nudge_x, nudge_y (text)











Position Adjustments Options

Position Adjustments

ggplot2 Extra settings

Coordinate systems

facet_wrap()
facet_grid()

theme()

color scales

Coordinate systems

coord_flip()

coord_polar()

facet_wrap()

Plot multi-panel

Plot multi-panel

facet_grid()

Plot multi-panel

Plot multi-panel

Plot multi-panel

Plot multi-panel

ggplot2 themes

  • theme_grey() (default)
  • theme_minimal()
  • theme_classic()
  • theme_bw()
  • theme_linedraw()
  • theme_light()
  • theme_dark()
  • theme_void()







You can also manually modify components of a theme. For more info look at theme()

Plot theme

Plot theme

Plot theme

Plot theme

ggplot2 color

RColorBrewer

  • scale_colour_brewer()
  • scale_fill_brewer()
  • scale_colour_distiller()
  • scale_fill_distiller()
  • scale_colour_fermenter()
  • scale_fill_fermenter()

Viridis

  • scale_colour_viridis_d()
  • scale_fill_viridis_d()
  • scale_colour_viridis_c()
  • scale_fill_viridis_c()
  • scale_colour_viridis_b()
  • scale_fill_viridis_b()

ggplot2 color

ggplot2 color

ggplot2 color

ggplot2 color

ggplot2 color

ggplot2 set scale color manual

“Less is more!”


― Ludwig Mies Van Der Rohe.

(don’t overplot)

Overplot

R Handbooks

R Language

Hands-On Programming with R
R for Data Science
Data Science at the Command Line

Visualization

ggplot2
Data Visualization
The R Graph Gallery
Fundamentals of Data Visualization

R Markdown

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com